// pages/shopping/index.js
import {
    store
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadMoreText: '加载中...', //底部文字
        list: [],
        option1: [{
                text: '默认排序',
                value: 0
            },
            {
                text: '价格排序',
                value: 1
            },
            {
                text: '销量排序',
                value: 2
            },
        ],
        option2: [{
                text: '排序方式',
                value: 'a'
            },
            {
                text: '从低到高',
                value: 'b'
            },
            {
                text: '从高到低',
                value: 'c'
            },
        ],
        option3: [],
        value1: 0,
        value2: 'a',
        value3: '0',
        orderby: '',
        orderway: '',
        name: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCategory()
        this.getList(1)
    },
    change1() {
        if (this.data.value1 == 1) {
            this.setData({
                orderby: 'price'
            })
        }
        if (this.data.value1 == 2) {
            this.setData({
                orderby: 'sales'
            })
        }
        if (this.data.value1 == 0) {
            this.setData({
                orderby: ''
            })
        }
        this.onPullDownRefresh()
    },
    change2() {
        if (this.data.value2 == 'a') {
            this.setData({
                orderway: ''
            })
        }
        if (this.data.value2 == 'b') {
            this.setData({
                orderway: 'asc'
            })
        }
        if (this.data.value2 == 'c') {
            this.setData({
                orderway: 'desc'
            })
        }
        this.onPullDownRefresh()
    },
    change3() {
        this.onPullDownRefresh()
    },
    // 获取分类
    getCategory() {
        store.category.treeList({}, res => {
            let datas = res.data
            let list = [{
                text: '商品分类',
                value: '0'
            }]
            datas.forEach(item => {
                list.push({
                    text: item.name,
                    value: item.id
                })
            })
            console.log(list)
            this.setData({
                option3:list
            })
        })
    },
    // 获取列表
    getList(page) {
        let that = this
        store.product.query({
            page: page,
            size: 10,
            orderby: that.data.orderby,
            orderway: that.data.orderway,
            name: that.data.name,
            category_id:that.data.value3
        }, res => {
            that.setData({
                total: res.data.total
            })
            if (res.data.list.length > 0) {
                that.setData({
                    list: that.data.list.concat(res.data.list) //合并数据
                })
                if (11 > res.data.list.length > 0) {
                    that.setData({
                        loadMoreText: '' //没有更多了
                    })
                }
            } else {
                if (page !== 1) {
                    that.setData({
                        loadMoreText: '暂无数据'
                    })
                } else {
                    that.setData({
                        list: [],
                        loadMoreText: '暂无数据'
                    })
                }
            }
            wx.stopPullDownRefresh();
        })
    },
    // 搜索
    onClick() {
        this.onPullDownRefresh()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})